﻿//AddGood.aspx.cs
////////////////////////////////////////
////////////////////////////////////////
//当管理员点击增加乐器时显示，显示要填的乐器的信息，并添加。
////////////////////////////////////////
////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_Addgood : System.Web.UI.Page
{
    CommonClass ccObj = new CommonClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        /////////////////////////////
        /////////////////////////////
        //绑定分类类别。
        /////////////////////////////
        /////////////////////////////
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {/*
        this.txtAllnumber.Text = "";
        this.txtBrand.Text = "";
        this.txtCategory.Text = "";
        this.txtCost.Text = "";
        this.txtCount.Text = "";
        this.txtDescription.Text = "";
        this.txtImg.Text = "";
        this.txtMusic.Text = "";
        this.txtName.Text = "";
        this.txtPrice.Text = "";
        this.txtStorenumber.Text = "";*/
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        ///////////////////////////////////////
        ///////////////////////////////////////
        /*将以下数据插入到乐器表中，若插入成功，则：
        this.txtName.Text.ToString().Trim();
        this.txtBrand.Text.ToString().Trim();
        this.txtCategory.Text.ToString().Trim();
        Decimal.Parse(this.txtCost.Text.ToString().Trim());
        Decimal.Parse(this.txtPrice.Text.ToString().Trim());
        float.Parse(this.txtCount.Text.ToString().Trim());
        int.Parse(this.txtAllnumber.Text.ToString().Trim());
        int.Parse(this.txtStorenumber.Text.ToString().Trim());
        this.txtImg.Text.ToString().Trim();
        this.txtMusic.Text.ToString().Trim();
        this.txtDescription.Text.ToString().Trim();
        */

        ///////////////////////////////////////
        ///////////////////////////////////////
        Response.Redirect("ManagerDefault.aspx");

        //////////////////////////////////
        //////////////////////////////////
        //若不成功，则：Response.Write(ccObj.MessageBox("插入失败！","ManagerDefault.aspx"));
        //////////////////////////////////
        //////////////////////////////////
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ManagerDefault.aspx");
    }

    private DataTable ReadExcelData(string strFileName)
    {
        string strSQLCommand = "SELECT   *   FROM   [Sheet1$]   ";
        string strCon;
        System.Data.OleDb.OleDbConnection conn;
        System.Data.OleDb.OleDbDataAdapter myCommand;
        DataTable dt = new DataTable();
        DataTable rd = new DataTable();
        rd.Columns.Add(new DataColumn("number", typeof(System.Int32)));
        rd.Columns.Add(new DataColumn("InstrumentName", typeof(System.String)));
        rd.Columns.Add(new DataColumn("Category", typeof(System.String)));
        rd.Columns.Add(new DataColumn("CostPrice", typeof(System.Double)));
        rd.Columns.Add(new DataColumn("SellPrice", typeof(System.Double)));
        rd.Columns.Add(new DataColumn("Discount", typeof(System.Double)));
        rd.Columns.Add(new DataColumn("Quantity", typeof(System.Int32)));
        rd.Columns.Add(new DataColumn("PictureUrl", typeof(System.String)));
        rd.Columns.Add(new DataColumn("Brand", typeof(System.String)));
        rd.Columns.Add(new DataColumn("Recommend", typeof(System.Int32)));
        rd.Columns.Add(new DataColumn("Description", typeof(System.String)));

        strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + strFileName + 
            ";Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";

        conn = new System.Data.OleDb.OleDbConnection(strCon);
        myCommand = new System.Data.OleDb.OleDbDataAdapter(strSQLCommand, strCon);
        myCommand.Fill(dt);
        DataRow dr;
        int i, j;
        for (i = 1; i < dt.Rows.Count; i++)
        {
            dr = rd.NewRow();
            for (j=0; j<11; j++)
            {
                dr[j] = dt.Rows[i][j];
            }
            rd.Rows.Add(dr);
        }
        
        return rd;
    }
    protected void BatchUpload(object sender, EventArgs e)
    {
        labelError.Visible = true;
        string serverpath;
        if (BatchBrowser.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(BatchBrowser.FileName);
            if (fileExt == ".xls" || fileExt == ".xlsx")
            {
                try
                {
                    serverpath = "C:\\" + BatchBrowser.FileName;
                    BatchBrowser.SaveAs(serverpath);
                    labelError.Text = "File name: " + BatchBrowser.PostedFile.FileName + " Upload Succeed!";
                }
                catch (Exception ex)
                {
                    labelError.Text = "ERROR: " + ex.Message.ToString();
                    return;
                }
            }
            else
            {
                labelError.Text = "Only Excel files allowed!";
                return;
            }
        }
        else
        {
            labelError.Text = "You have not specified a file.";
            return;
        }

        // 读excel文件
        DataTable dt = ReadExcelData(serverpath);
        
        grid.DataSource = dt;
        grid.DataBind();

    }
}
